//
//  DSFriendsRecommendDetailModel.m
//  budejie
//
//  Created by tangQiang on 8/27/16.
//  Copyright © 2016 Dason. All rights reserved.
//

#import "DSFriendsRecommendDetailModel.h"

@implementation DSFriendsRecommendDetailModel

#pragma mark - 自定义构造方法
+ (instancetype)friendsRecommendDetailModelWithDic:(NSDictionary *)dic {
    DSFriendsRecommendDetailModel *friendsRecommendDetailModel = [[self alloc]init];
    [friendsRecommendDetailModel setValuesForKeysWithDictionary:dic];
    return friendsRecommendDetailModel;
}

#pragma mark - 重写 setValue
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    if ([key isEqualToString:@"id"]) {
        self.ID = value;
    }
}

/**
 *  下载数据
 */
+ (void)downloadDataWithDictionary:(NSDictionary *)paramaters successBlock:(void(^)(NSArray<DSFriendsRecommendDetailModel *> *friendsRecommendDetailModels)) successBlock failBlock:(void(^)(NSError *error)) failBlock {
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://api.budejie.com"]];
    [manager GET:@"api/api_open.php" parameters:paramaters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //NSLog(@"%@",responseObject);
        NSDictionary *dic = responseObject;
        __block NSArray *arr = nil;
        [paramaters enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if ([key isEqualToString:@"category_id"]) {
                arr = dic[@"list"];
            } else {
                arr = dic[@"top_list"];
            }
        }];
    
        NSMutableArray<DSFriendsRecommendDetailModel *> *arrM = [NSMutableArray arrayWithCapacity:arr.count];
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            DSFriendsRecommendDetailModel *friendsRecommendDetailModel = [self friendsRecommendDetailModelWithDic:obj];
            [arrM addObject:friendsRecommendDetailModel];
        }];
        if (successBlock) {
            successBlock(arrM.copy);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failBlock) {
            failBlock(error);
        }
    }];

}

@end


